<?php

namespace App\Providers;


use function foo\func;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;

class ValidationServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // 手机号验证
        Validator::extend('mobile', function ($attribute, $value, $parameters, $validator) {
            if (Str::startsWith($value, 'fake:')) {
                return true;
            }
            return preg_match('/^1\d{10}$/', $value) ? true : false;
        });
        Validator::replacer('mobile', function ($message, $attribute, $rule, $parameters) {
            return '手机号格式错误 ！';
        });


        // 金额验证，2位小数
        Validator::extend('decimal2d', function ($attribute, $value, $parameters, $validator) {
            return preg_match('/^(([1-9]+[0-9]*)|(0))(\.[0-9]{2})?$/', $value) ? true : false;
        });
        Validator::replacer('decimal2d', function ($message, $attribute, $rule, $parameters) {
            return $attribute . '格式错误,必须为正数且保留两位小数！';
        });

        //1位小数
        Validator::extend('decimal1d', function ($attribute, $value, $parameters, $validator) {
            return preg_match('/^(([1-9]+[0-9]*)|(0))(\.[0-9])?$/', $value) ? true : false;
        });
        Validator::replacer('decimal1d', function ($message, $attribute, $rule, $parameters) {
            return $attribute . '格式错误,必须为正数且保留两位小数！';
        });

        //int数组
        Validator::extend('array[integer]', function ($message, $attribute, $rule, $parameters) {
            if (!is_array($attribute)) {
                return false;
            }
            foreach ($attribute as $k => $v) {
                if (!is_integer((int)$v)) {
                    return false;
                }
            }
            return true;
        });
        Validator::replacer('array[integer]', function ($message, $attribute, $rule, $parameters) {
            return '数组中的值必须为整数数组';
        });

        //唯一验证，适用模型新增和更新情况下验证 参数 是否唯一
        Validator::extend('single_unique', function ($message, $attribute, $rule, \Illuminate\Validation\Validator $parameters) {
            $params = $parameters->getData();
            list($table, $column) = $rule;
            $foundId = DB::table($table)->whereNull('deleted_at')->where($column, $params[$column])->value('id');
            //一.没发现则通过验证
            //二.参数集中包含ID字段且ID字段的值等于发现值亦验证通过
            if (is_null($foundId) || (isset($params['id']) && $params['id'] == $foundId)) {
                return true;
            }
            return false;
        });
        Validator::replacer('single_unique', function ($message, $attribute, $rule, $parameters) {
            return '数据重复:' . $attribute;
        });

        //联合唯一验证，适用模型新增和更新情况下验证 参数组合 是否唯一
        Validator::extend('union_unique', function ($message, $attribute, $rule, \Illuminate\Validation\Validator $parameters) {
            $table = array_shift($rule);
            $filter = [];
            $params = $parameters->getData();
            foreach ($rule as $r) {
                if (!isset($params[$r])) {
                    return false;
                }
                $filter[$r] = $params[$r];
            }
            $foundId = DB::table($table)->whereNull('deleted_at')->where($filter)->value('id');
            //一.没发现则通过验证
            //二.参数集中包含ID字段且ID字段的值等于发现值亦验证通过
            if (is_null($foundId) || (isset($params['id']) && $params['id'] == $foundId)) {
                return true;
            }
            return false;
        });
        Validator::replacer('union_unique', function ($message, $attribute, $rule, $parameters) {
            return '数据重复:' . $attribute;
        });


        //外键等于某个值，更新资源之前要检查是否为资源的所有者
        Validator::extend('has', function ($message, $attribute, $rule, $parameters) {
            list($table, $condition) = $rule;
            list($column, $value) = explode('=', $condition);

            return DB::table($table)->where('id', $attribute)->value($column) == $value;
        });
        Validator::replacer('has', function ($message, $attribute, $rule, $parameters) {
            return '不是该资源所有者';
        });


    }

}
